{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ded0b044",
   "metadata": {},
   "source": [
    "## 1.  本周任务\n",
    "### 1.1 优化ASR机器人\n",
    "### 1.2 连接图灵机器人\n",
    "### 1.3 语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6c9f082",
   "metadata": {},
   "source": [
    "## 2. 优化ASR机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2c6d4f23",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'LlfEgXvRWxajUe44Q3eiSGqI'\n",
    "SECRET_KEY = 'gKxHMKamyWP9U5XX7KaknsnMIVXhC3Vi'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "30bd84d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import fetch_token,asr\n",
    "my_token = fetch_token(API_KEY,SECRET_KEY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dfcdba39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.699f84cc3dae0fcb41804ee7412493b0.2592000.1655988194.282335-26256000'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8a5a9924",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b52b1938",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.203923\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'在干嘛？'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr_result = eval(asr(my_token,'1.wav'))['result'][0]\n",
    "asr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbaf0fa5",
   "metadata": {},
   "source": [
    "## 3. 连接图灵机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "308e5f63",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "189b8173",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c74879ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "TL_url = 'http://openapi.turingapi.com/openapi/api/v2' # 请求的调用url\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"你觉得你聪明嘛\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": \"d5d5bb66f66d4da8bedb378df7adfa70\",\n",
    "        \"userId\": \"767223\"\n",
    "    }\n",
    "}\n",
    "\n",
    "# 发起请求\n",
    "r = requests.post(url = TL_url, data = json.dumps(params))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b86a0ef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0826350c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'对哇'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4339a41f",
   "metadata": {},
   "source": [
    "## 4. 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0c92d5b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pydub in d:\\anaconda\\lib\\site-packages (0.25.1)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\anaconda\\lib\\site-packages\\pydub\\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\n",
      "  warn(\"Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\", RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "!pip install pydub\n",
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "84267c3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import tts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "675b39fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%E4%BD%A0%E7%9A%84%E5%A3%B0%E9%9F%B3%E5%BE%88%E9%9A%BE%E5%90%AC\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.699f84cc3dae0fcb41804ee7412493b0.2592000.1655988194.282335-26256000&tex=%25E4%25BD%25A0%25E7%259A%2584%25E5%25A3%25B0%25E9%259F%25B3%25E5%25BE%2588%25E9%259A%25BE%25E5%2590%25AC&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "tts(my_token,\"你的声音很难听\")\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cee9b535",
   "metadata": {},
   "source": [
    "## 5. 语音对话机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c9f68e15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.458851\n",
      "对哇\n",
      "%E5%AF%B9%E5%93%87\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.d2565e5257be7b7a541d496b86ce029c.2592000.1656517036.282335-26256000&tex=%25E5%25AF%25B9%25E5%2593%2587&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition  # 音频录制模块\n",
    "from baidu_speech import fetch_token,asr,tts  # 百度语音系统模块\n",
    "from Turing import turing  # 图灵机器人\n",
    "from pydub import AudioSegment  # 播放音频模块\n",
    "from pydub.playback import play\n",
    "\n",
    "# 各种key\n",
    "API_KEY = 'LlfEgXvRWxajUe44Q3eiSGqI'\n",
    "SECRET_KEY = 'gKxHMKamyWP9U5XX7KaknsnMIVXhC3Vi'\n",
    "TL_key = 'd5d5bb66f66d4da8bedb378df7adfa70'\n",
    "\n",
    "# 1.录制我们的语音存放在“1.wav”文件中\n",
    "r = speech_recognition.Recognizer()\n",
    "# 打开电脑麦克风\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2.运用百度asr接口将音频转换为文本\n",
    "my_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_result = eval(asr(my_token,'1.wav'))['result'][0]\n",
    "\n",
    "# 3.将文本发送给图灵机器人，获取文本回复\n",
    "TL_result = turing(TL_key,asr_result)\n",
    "print(TL_result)\n",
    "\n",
    "# 4.将图灵机器人的文本回复通过百度tts接口转换成音频\n",
    "tts(my_token,TL_result)\n",
    "\n",
    "# 5.通过play模块进行音频播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n",
    "\n",
    "# finished"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c09373dc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
